<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Watson\Rememberable\Rememberable;

class Video extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;
    use Rememberable;

    protected $table = 'video';

    protected $rememberCacheTag = 'video_cache';

    protected $cache_expire_in_minutes = 1440;


    /**
     * 获取首页推荐文章
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-02-27
     * @return void
     */
    public function getRecommendVideo()
    {
        return  Video::where(['status' => 1, 'recommend' => 1])
            // ->remember($this->cache_expire_in_minutes, $this->rememberCacheTag)
            ->select('id', 'title', 'video_cover as cover_pic', 'created_at', 'sort')
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->limit(10)
            ->get();
    }


    /**
     * 获取视频列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-02-28
     * @param array $payload
     * @return void
     */
    public function getVideoList($payload)
    {
        $where = function ($query) use ($payload) {

            $query->where('status', 1);

            if (isset($payload['title']) && $payload['title'] != '') {
                $query->where('title', 'like', '%' . $payload['title'] . '%');
            }
        };

        return Video::where($where)
            ->select('id', 'title', 'video_cover as cover_pic', 'description', 'created_at', 'sort')
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate($payload['limit']);
    }


    /**
     * 获取视频详情与推荐文章
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-05-10
     * @param [type] $id
     * @return void
     */
    public function getVideoDetail($id)
    {
        $detail['video'] = Video::query()->where(['id' => $id, 'status' => 1])->select('id', 'title', 'keywords', 'description', 'content', 'publish_time', 'status', 'recommend', 'author', 'out_url', 'video_cover', 'video', 'source')->first();

        $detail['article'] = Article::where(['status' => 1, 'recommend' => 1])->select('id', 'title', 'cover_pic')->limit(6)->orderBy('created_at', 'desc')->get();

        return $detail;
    }
}
